package com.vf.admin.miaodong.controller;

import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppBanner;
import com.vf.admin.miaodong.service.AfterBannerService;
import com.vf.admin.utils.Base64Image.FileUtil;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 轮播图管理
 */
@Controller
@RequestMapping("/after/banner")
public class AfterBannerController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    AfterBannerService afterBannerService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * @Description 添加轮播图
     * @Param: [appBanner]
     * @return: com.vf.admin.common.entity.Result
     * @Author: ljx
     * @Date: 2021/10/21
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result addBanner(AppBanner appBanner) {
        try {
            if (StringUtils.isEmpty(appBanner.getTitle())) {
                return errorJSON("标题不能为空");
            }
            if (StringUtils.isEmpty(appBanner.getLeixing())) {
                return errorJSON("类型不能为空");
            }
            if (StringUtils.isEmpty(appBanner.getImage())) {
                return errorJSON("图片不能为空");
            }
            if (StringUtils.isEmpty(appBanner.getStatus())) {
                return errorJSON("状态不能为空");
            }

            if (appBanner.getId() != null) {
                AppBanner appBanner1 = afterBannerService.get(appBanner.getId());
                appBanner1.setRemarks(appBanner.getRemarks());
                appBanner1.setSort(appBanner.getSort());
                appBanner1.setTitle(appBanner.getTitle());
                appBanner1.setLeixing(appBanner.getLeixing());
                appBanner1.setWeizhi(appBanner.getWeizhi());
                appBanner1.setUpdatetime(new Date());
                appBanner1.setStatus(appBanner.getStatus());
                //处理图片
                String newfilePath = "";

                Map map = FileUtil.aliyunOSSUpload(appBanner.getImage(), "");
                if (map.get("st").equals("0")) {
                    newfilePath = (String) map.get("data");
                }
                appBanner1.setImage(newfilePath);
                afterBannerService.save(appBanner1);
            } else {
                //处理图片
                String newfilePath = "";
                Map map = FileUtil.aliyunOSSUpload(appBanner.getImage(), "");
                if (map.get("st").equals("0")) {
                    newfilePath = (String) map.get("data");
                }
                appBanner.setImage(newfilePath);
                appBanner.setWeizhi(appBanner.getWeizhi());
                appBanner.setCreatetime(new Date());
                appBanner.setUpdatetime(appBanner.getCreatetime());
                afterBannerService.save(appBanner);
            }
            redisUtil.del(RedisKeys.CACHE_LUNBOTU+1);
            redisUtil.del(RedisKeys.CACHE_LUNBOTU+2);
            return sucessJSON("修改成功");
        } catch (Exception e) {
            return errorJSON("系统繁忙");
        }
    }

    /**
     * 详情
     */
    @RequestMapping("/detail")
    public String detail(Integer id) {
        try {
            AppBanner appBanner = afterBannerService.get(id);
            Map<Integer, String> bannerTypeMap = Constants.getBannerTypeMap();
            Map<Integer, String> deliverystatusMap = Constants.getdeliverystatusMap();
            Map<Integer, String> statusMap = Constants.getstatusMap();
            request.setAttribute("bannerTypeMap", bannerTypeMap);
            request.setAttribute("deliverystatusMap", deliverystatusMap);
            request.setAttribute("statusMap", statusMap);
            request.setAttribute("appBanner", appBanner);
            return this.sucess("/web/after/banner/edit");
        } catch (Exception e) {
            logger.error("获取轮播图异常", e);
            return this.error("系统繁忙");
        }
    }

    /**
     * 根据各种条件 获取banner
     */
    @RequestMapping("/getbanner")
    public String getbanner(SplitPageBean splitPageBean, String leixing) {
        try {
            Map<String, Object> paramap = new HashMap<>();
            if (StringEx.isNotNull(leixing)) {
                paramap.put("leixing", leixing);
            }
            List<AppBanner> searchBanner = afterBannerService.list(paramap, splitPageBean);
            Map<Integer, String> bannerTypeMap = Constants.getBannerTypeMap();
            Map<Integer, String> deliverystatusMap = Constants.getdeliverystatusMap();
            Map<Integer, String> statusMap = Constants.getstatusMap();

            request.setAttribute("list", searchBanner);
            request.setAttribute("bannerTypeMap", bannerTypeMap);
            request.setAttribute("deliverystatusMap", deliverystatusMap);
            request.setAttribute("statusMap", statusMap);
            request.setAttribute("splitPage", splitPageBean);
            //mm.put("list",searchBanner);
            return this.sucess("/web/after/banner/list");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

    /**
     * 删除banner
     */
    @RequestMapping("/delBanner")
    @ResponseBody
    public Result delBanner(Integer id) {
        try {
            if (id == null || id <= 0) {
                return this.errorJSON("轮播图ID不能为空！");
            }
            afterBannerService.del(id);
            redisUtil.del(RedisKeys.CACHE_LUNBOTU);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }
}
